package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Forbidden;
import com.tbit.uqbike.object.pojo.ForbiddenAddDTO;
import com.tbit.uqbike.object.pojo.dto.ForbiddenUpdateDTO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.ForbiddenService;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 禁行区配置
 *
 * @Author:BUGTian
 * @DATE: 2023/1/8
 */
@RestController
@RequestMapping("/forbidden")
public class ForbiddenController {
    @Autowired
    private ForbiddenService forbiddenService;


    /**
     * 分页查询禁行区
     *
     * @param page      当前页码
     * @param size      每页显示的行数
     * @param accountId 区域id
     * @param mapType   地图类型  0:原始经纬度,1:百度经纬度,2:谷歌/高德/腾讯经纬度
     * @param name      禁行区名称
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<Forbidden>> queryPage(@NotNull Integer page, @NotNull Integer size, @NotNull Integer accountId, Integer mapType, String name) {
        Assert.notNulls("参数有误", page, size, accountId);
        mapType = mapType == null ? MapConstant.MAP_GOOGLE : mapType;
        PageResult<Forbidden> pageResult = forbiddenService.queryPage(page, size, accountId, name, mapType);
        return Result.success(pageResult);
    }

    /**
     * 新增禁行区
     *
     * @param dto
     * @return
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.FORBIDDEN, methods = MethodEnum.FORBIDDEN_ADD)
    public Result<String> add(@RequestBody ForbiddenAddDTO dto) {
        Assert.notNulls("参数有误", dto.getAccountId(), dto.getName(), dto.getPointsC(), dto.getEnable());
        if (dto.getName().length() > 100){
            throw new BaseException("禁行区名称过长");
        }else if (StrUtil.isNotEmpty(dto.getRemark()) && dto.getRemark().length() > 255){
            throw new BaseException("禁行区备注过长");
        }
        forbiddenService.add(dto);
        return Result.success();
    }

    /**
     * 修改禁行区
     *
     * @param dto
     * @return
     */
    @PutMapping("/update")
    @OperateLog(module = ModuleEnum.FORBIDDEN, methods = MethodEnum.FORBIDDEN_UPDATE)
    public Result<String> update(@RequestBody ForbiddenUpdateDTO dto) {
        Assert.notNulls("参数有误", dto.getForbiddenId(), dto.getAccountId(), dto.getName(), dto.getPointsC(), dto.getEnable(), dto.getEnable());
        if (dto.getName().length() > 100){
            throw new BaseException("禁行区名称过长");
        }else if (StrUtil.isNotEmpty(dto.getRemark()) && dto.getRemark().length() > 255){
            throw new BaseException("禁行区备注过长");
        }
        forbiddenService.updateForbidden(dto);
        return Result.success();
    }


    /**
     * 删除禁行区
     *
     * @param forbiddenId
     * @return
     */
    @DeleteMapping("/del")
    @OperateLog(module = ModuleEnum.FORBIDDEN, methods = MethodEnum.FORBIDDEN_DEL)
    public Result<String> del(@NotNull Integer forbiddenId) {
        forbiddenService.removeById(forbiddenId);
        return Result.success();
    }


}
